package com.ccx.coachsite.bus;

import com.ccx.coachsite.http.BaseResp;
import com.ccx.coachsite.http.HttpCallBack;
import com.ccx.coachsite.task.TaskType;
import com.ccx.coachsite.util.StringUtil;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.widget.Toast;

public class BaseBus implements HttpCallBack<BaseResp>,ILoadBus
{
	protected Activity mActivity;
	protected ProgressDialog mProgress;
	protected String processInfo;
	protected String successInfo;
	protected String errorInfo;
	protected String errorEmptyInfo;

	protected Context mContext;
	private boolean isShowTip;
	
	public boolean isShowTip() {
		return isShowTip;
	}

	public void setShowTip(boolean isShowTip) {
		this.isShowTip = isShowTip;
	}
	
	public BaseBus(Activity activity, String pinfo)
	{
		isShowTip = true;
		mActivity = activity;
		processInfo = pinfo;
		successInfo = "操作成功";
		errorInfo = "操作失败";
		mContext = mActivity;
	}
	
	public BaseBus(Context context)
	{
		isShowTip = true;
		mContext = context;
	}
	
	public String getErrorEmpty()
	{
		if(StringUtil.isEmpty(errorEmptyInfo))
			return errorInfo;
		else
			return errorEmptyInfo;
	}
	
	protected void onCallback(boolean isSucces, int taskType, BaseResp response, String responseJson)
	{
		
	}
	
	public void onCompleted(boolean isSuccess, String desc)
	{
		
	}

	@Override
	public void callBack(int taskType, BaseResp response, String responseJson) {
		dismissProgress();

		BaseResp resp = response;
		String toaskInfo = successInfo;
		if (!resp.isSuccess()) {
			if(!StringUtil.isEmpty(resp.getDesc()) && !resp.getDesc().equals("null"))
				toaskInfo = errorInfo+","+resp.getDesc();
			else
				toaskInfo = getErrorEmpty();
		}
		if(isShowTip)
			Toast.makeText(mContext, toaskInfo, Toast.LENGTH_SHORT).show();
		
		onCallback(resp.isSuccess(), taskType, response, responseJson);
	}

	protected void showProgress() {
		if (null == mProgress) {
			mProgress = new ProgressDialog(mContext);
			mProgress.setMessage(processInfo);
		}
		mProgress.show();
	}

	protected void dismissProgress() {
		if (null != mProgress) {
			mProgress.dismiss();
			mProgress = null;
		}
	}
	
	public String getProcessInfo() {
		return processInfo;
	}

	public void setProcessInfo(String processInfo) {
		this.processInfo = processInfo;
	}

	public String getSuccessInfo() {
		return successInfo;
	}

	public void setSuccessInfo(String successInfo) {
		this.successInfo = successInfo;
	}

	public String getErrorInfo() {
		return errorInfo;
	}

	public void setErrorInfo(String errorInfo) {
		this.errorInfo = errorInfo;
	}
}
